<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/29 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\common\service;
use Guzzle\Http\EntityBody;
use Qcloud\Cos\Client;
use think\facade\Cache;
/**
 * TODO 腾讯云COS文件上传
 * Class COS
 */
class COS
{
    protected static $accessKey;

    protected static $secretKey;

    protected static $auth = null;

    //TODO 空间域名 Domain
    protected static $uploadUrl;

    //TODO 存储空间名称  公开空间
    protected static $storageName;

    //TODO COS使用  所属地域
    protected static $storageRegion;

    /**
     * TODO 初始化
     * @return null|Client
     * @throws \Exception
     */
    protected static function autoInfo()
    {
        if (($storageRegion = Cache::get('storageRegion')) && ($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))) {
            self::$accessKey = $accessKey;
            self::$secretKey = $secretKey;
            self::$uploadUrl = $uploadUrl;
            self::$storageName = $storageName;
            self::$storageRegion = $storageRegion;
        } else {
            self::$accessKey = trim(sysconfig("upload",'accessKey'));
            self::$secretKey = trim(sysconfig("upload",'secretKey'));
            self::$uploadUrl = trim(sysconfig("upload",'uploadUrl')) . '/';
            self::$storageName = trim(sysconfig("upload",'storage_name'));
            self::$storageRegion = trim(sysconfig("upload",'storage_region'));
            Cache::set('accessKey', self::$accessKey);
            Cache::set('secretKey', self::$secretKey);
            Cache::set('uploadUrl', self::$uploadUrl);
            Cache::set('storageName', self::$storageName);
            Cache::set('storageRegion', self::$storageRegion);
        }
        if (!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName) {
            exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
        }
        if (self::$auth == null) {
            self::$auth = new Client([
                'region' => self::$storageRegion,
                'credentials' => [
                    'secretId' => self::$accessKey,
                    'secretKey' => self::$secretKey,
                ]
            ]);
        }
        return self::$auth;
    }

    /**
     * TODO 文件上传 名称
     * @param string $filename
     * @return string
     */
    public static function uploadImage($filename = 'image')
    {
        $request = app('request');
        $file = $request->file($filename);
        $filePath = $file->getRealPath();
        $ext = $file->getOriginalExtension();
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
        try {
            self::autoInfo();
            return [self::$uploadUrl . $key, self::$auth->putObject([
                'Bucket' => self::$storageName,
                'Key' => $key,
                'Body' => fopen($filePath, 'rb')
            ])];
        } catch (\Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    /**
     * TODO 文件上传 内容
     * @param $key
     * @param $content
     * @return string
     */
    public static function uploadImageStream($key, $content)
    {
        try {
            self::autoInfo();
            return [self::$uploadUrl . $key, self::$auth->putObject([
                'Bucket' => self::$storageName,
                'Key' => $key,
                'Body' => $content
            ])];
        } catch (\Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    /**
     * TODO 删除资源
     * @param $key
     * @return mixed
     */
    public static function delete($key)
    {
        try {
            self::autoInfo();
            return self::$auth->deleteObject([
                'Bucket' => self::$storageName,
                'Key' => $key
            ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * TODO 转为文件流
     * @param $resource
     * @return EntityBody
     */
    public static function resourceStream($resource)
    {
        return EntityBody::factory($resource)->__toString();
    }
}